﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace YNLibWin
{
    public partial class frmGroupAddEdit : Form
    {
        WinLib.BLL.SSO_UserBaseInfo bll = new WinLib.BLL.SSO_UserBaseInfo();
        WinLib.Model.SSO_UserBaseInfo model = new WinLib.Model.SSO_UserBaseInfo();
        public bool flagProvince, flagCity;//ComboBox是否初始化完成标示,避免ComboBox绑定数据源时触发SelectedIndexChanged事件
        /// <summary>
        /// 用户ID
        /// </summary>
        public int SYSID = 0;
        public frmGroupAddEdit()
        {
            InitializeComponent();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            #region 验证数据
            if (string.IsNullOrEmpty(this.txtUserCode.Text))
            {
                MessageBox.Show("登录账号不能为空！", "注意", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.txtUserCode.Focus();
                return;
            }
            if (string.IsNullOrEmpty(this.txtRealName.Text))
            {
                MessageBox.Show("集团名称不能为空！", "注意", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.txtRealName.Focus();
                return;
            }
            if (string.IsNullOrEmpty(this.txtIPFrom.Text))
            {
                MessageBox.Show("起始IP不能为空！", "注意", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.txtIPFrom.Focus();
                return;
            }
            if (string.IsNullOrEmpty(this.txtIPTo.Text))
            {
                MessageBox.Show("结束IP不能为空！", "注意", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.txtIPTo.Focus();
                return;
            }
            if (string.IsNullOrEmpty(this.txtEmail.Text))
            {
                MessageBox.Show("E-mail不能为空！", "注意", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.txtEmail.Focus();
                return;
            }
            else
            {
                if (!WinLib.Common.PageValidate.IsEmail(this.txtEmail.Text.Trim()))
                {
                    MessageBox.Show("E-mail格式不正确！", "注意", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    this.txtEmail.Focus();
                    return;
                }
            }
            if (string.IsNullOrEmpty(this.txtLoginCount.Text))
            {
                MessageBox.Show("登录并发数为空！", "注意", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.txtLoginCount.Focus();
                return;
            }
            else
            {
                if (!WinLib.Common.PageValidate.IsInt(this.txtLoginCount.Text))
                {
                    MessageBox.Show("登录并发数必须为整数！", "注意", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    this.txtLoginCount.Focus();
                    return;
                }
            }
            #endregion
            SetModel();
            if (SYSID == 0)
            {
                if (bll.Add(model) > 0)
                {
                    MessageBox.Show("添加成功");
                }
            }
            else
            {
                if (bll.Update(model))
                {
                    MessageBox.Show("修改成功");
                }
            }
        }

        private void txtUserCode_Leave(object sender, EventArgs e)
        {

        }

        private void frmUserAddEdit_Load(object sender, EventArgs e)
        {
            InitInfo();
            BindData();
        }
        /// <summary>
        /// 初始化页面
        /// </summary>
        protected void InitInfo()
        {
            //#region 绑定证件类别
            //DataTable dtCardType = new DataTable();
            //dtCardType.Columns.Add("fieldName");
            //dtCardType.Columns.Add("fieldValue");
            //DataRow dr = dtCardType.NewRow();
            //dtCardType.Rows.Add("身份证", "01");
            //dtCardType.Rows.Add("军官证", "02");
            //dtCardType.Rows.Add("护照", "03");
            //dtCardType.Rows.Add("港澳通行证", "04");
            //dtCardType.Rows.Add("台湾通讯证", "05");
            //this.cboCardType.DataSource = dtCardType;
            //this.cboCardType.DisplayMember = "fieldName";
            //this.cboCardType.ValueMember = "fieldValue"; 
            //#endregion

            //#region 绑定学历
            //DataTable dtEducation = new DataTable();
            //dtEducation.Columns.Add("fieldName");
            //dtEducation.Columns.Add("fieldValue");
            //DataRow drEducation = dtEducation.NewRow();
            //dtEducation.Rows.Add("小学及以下", "小学及以下");
            //dtEducation.Rows.Add("初中", "初中");
            //dtEducation.Rows.Add("高中", "高中");
            //dtEducation.Rows.Add("大学", "大学");
            //dtEducation.Rows.Add("硕士", "硕士");
            //dtEducation.Rows.Add("博士及以上", "博士及以上");
            //this.cboEducation.DataSource = dtEducation;
            //this.cboEducation.DisplayMember = "fieldName";
            //this.cboEducation.ValueMember = "fieldValue";
            //this.cboEducation.SelectedIndex = 3;//默认选中大学
            //#endregion

            //#region 绑定职称
            //DataTable dtPosition = new DataTable();
            //dtPosition.Columns.Add("fieldName");
            //dtPosition.Columns.Add("fieldValue");
            //DataRow drPosition = dtPosition.NewRow();
            //dtPosition.Rows.Add("无职称", "无职称");
            //dtPosition.Rows.Add("初级", "初级");
            //dtPosition.Rows.Add("中级", "中级");
            //dtPosition.Rows.Add("副高", "副高");
            //dtPosition.Rows.Add("正高", "正高");
            //this.cboPosition.DataSource = dtPosition;
            //this.cboPosition.DisplayMember = "fieldName";
            //this.cboPosition.ValueMember = "fieldValue";
            //#endregion

            #region 初始化省市区/县
            flagProvince = false;
            string sqlProvince = "select name as fieldName, codeID as fieldValue from [tbl_Region] where parentID='0'";
            DataTable dtProvince = WinLib.DBUtility.DbHelperSQL.ExecuteQuery(sqlProvince);
            this.cboProvince.DataSource = dtProvince;
            this.cboProvince.DisplayMember = "fieldName";
            this.cboProvince.ValueMember = "fieldValue";
            //在最前面插入一个选项
            DataRow drProvince0 = dtProvince.NewRow();
            drProvince0["fieldName"] = "--请选择省份--";
            drProvince0["fieldValue"] = "";
            dtProvince.Rows.InsertAt(drProvince0, 0);
            this.cboProvince.SelectedIndex = 0;
            flagProvince = true;

            flagCity = false;
            InitCity();//初始化市

            InitCounty();//初始化区/县
            #endregion

            #region 初始化权限组
            string sqlRole = "select rolename,sysid from SSO_RoleInfo where recflag='1'";
            DataTable dtRole = WinLib.DBUtility.DbHelperSQL.ExecuteQuery(sqlRole);
            this.cboRole.DataSource = dtRole;
            this.cboRole.DisplayMember = "rolename";
            this.cboRole.ValueMember = "sysid";
            this.cboRole.SelectedIndex = 0; 
            #endregion

            #region 绑定状态
            DataTable dtUserFlag = new DataTable();
            dtUserFlag.Columns.Add("fieldName");
            dtUserFlag.Columns.Add("fieldValue");
            DataRow drUserFlag = dtUserFlag.NewRow();
            dtUserFlag.Rows.Add("未审核", "1");
            dtUserFlag.Rows.Add("已审核", "2");
            dtUserFlag.Rows.Add("停用", "3");
            this.cboUserFlag.DataSource = dtUserFlag;
            this.cboUserFlag.DisplayMember = "fieldName";
            this.cboUserFlag.ValueMember = "fieldValue";
            #endregion
        }
        /// <summary>
        /// 初始化市
        /// </summary>
        protected void InitCity()
        {
            DataTable dtCity0 = new DataTable();
            dtCity0.Columns.Add("fieldName");
            dtCity0.Columns.Add("fieldValue");
            DataRow drCity0 = dtCity0.NewRow();
            dtCity0.Rows.Add("--请选择市--", "");
            this.cboCity.DataSource = null;
            this.cboCity.Items.Clear();
            this.cboCity.DataSource = dtCity0;
            this.cboCity.DisplayMember = "fieldName";
            this.cboCity.ValueMember = "fieldValue";
            this.cboCity.SelectedIndex = 0;
        }
        /// <summary>
        /// 初始化区/县
        /// </summary>
        protected void InitCounty()
        {
            DataTable dtCounty0 = new DataTable();
            dtCounty0.Columns.Add("fieldName");
            dtCounty0.Columns.Add("fieldValue");
            DataRow drCounty0 = dtCounty0.NewRow();
            dtCounty0.Rows.Add("--请选择区/县--", "");
            this.cboCounty.DataSource = null;
            this.cboCounty.Items.Clear();
            this.cboCounty.DataSource = dtCounty0;
            this.cboCounty.DisplayMember = "fieldName";
            this.cboCounty.ValueMember = "fieldValue";
            this.cboCounty.SelectedIndex = 0;
        }


        private void BindData()
        {
            if (SYSID == 0)
            {
                //添加状态
                this.Text = "添加集团用户";
                this.btnSave.Text = "添加";
            }
            else
            {
                //编辑状态
                this.Text = "修改集团用户信息";
                this.btnSave.Text = "修改";

                model = bll.GetModel(SYSID);
                if (model != null)
                {
                    SetUserInfo();
                }
                else
                {
                    MessageBox.Show("没有此用户信息", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        #region 给Model赋值
        /// <summary>
        /// 给Model赋值
        /// </summary>
        public void SetModel()
        {
            model.UserCode = this.txtUserCode.Text;
            model.RealName = this.txtRealName.Text;
            if (!string.IsNullOrEmpty(this.txtIPFrom.Text))
            {
                model.password = WinLib.Common.Encrypt.MD5(this.txtIPFrom.Text);
                model.RealPassword = this.txtIPFrom.Text;//保存明文密码 
            }
            model.PostCode = this.txtPostCode.Text;
            model.UnitAddress = this.txtAddress.Text;
            model.Email = this.txtEmail.Text;
            model.JoinDate = this.dtpJoinDate.Value;
            model.EndDate = this.dtpEndDate.Value;
            model.UserFlag = Convert.ToInt32(this.cboUserFlag.SelectedValue);
            model.CardType = "01";
            model.IdentificationCard = "";
                model.Sex = 0;
            model.Telephone = this.txtTelePhone.Text;
            model.Question = "";
            model.Answer = "";
            model.UnitName = "";// this.UnitName.Text;
            model.UserDescript = "";// this.UserDescript.Text;
            model.DepartName = "";// this.DepartName.Text;
            model.IsManager = 2;
            model.IsHasExtend = 0;
            model.IsHasMap = 0;
            model.IsHasRole = 0;
            model.CreateTime = DateTime.Now;
            model.RECFLAG = 1;


            model.Birth = DateTime.Now;
            model.Positions = "";
            model.Education = "";

            model.CellPhone = "";
            model.LibraryCard = "";
            model.Jobs = "";
            model.UserRole = 3;
            model.RoleRight = Convert.ToInt32(this.cboRole.SelectedValue);

            model.Country = this.txtCountry.Text.Trim();
            model.Province = this.cboProvince.Text;
            model.City = this.cboCity.Text;
            model.County = this.cboCounty.Text;
            model.Fax = this.txtFax.Text.Trim();//传真
            model.LoginCount = Convert.ToInt32(this.txtLoginCount.Text);
            model.IPFrom = this.txtIPFrom.Text;
            model.IPTo = this.txtIPTo.Text;
            model.RoamFlag = "Y";//开通数字馆标志，Y	申请开通；N	未申请开通
            model.AppCode = "ynlib";//应用系统编码(国图接口）
            model.RegCode = string.Empty;//注册激活码
            model.ReqCode = "";//请求操作识别码(国图接口）
            model.IP = "";
        }
        #endregion
        /// <summary>
        /// 填充用户信息
        /// </summary>
        protected void SetUserInfo()
        {
            this.txtUserCode.Text = model.UserCode;
            this.txtRealName.Text = model.RealName;
            this.txtPostCode.Text = model.PostCode;
            this.txtAddress.Text = model.UnitAddress;
            this.txtEmail.Text = model.Email;
            this.dtpJoinDate.Value = Convert.ToDateTime(model.JoinDate);
            this.dtpEndDate.Value = Convert.ToDateTime(model.EndDate);
            this.cboUserFlag.SelectedValue = model.UserFlag.ToString();
            //this.cboCardType.SelectedValue = model.CardType;
            //this.txtIdentificationCard.Text = model.IdentificationCard;
            //this.MobilPhone.Text = model.MobilphoenNumber;
            //if (model.Sex == 0)
            //{
            //    this.rbtnSex1.Checked = true;
            //}
            //else
            //{
            //    this.rbtnSex2.Checked = true;
            //}
            this.txtTelePhone.Text = model.Telephone;
            //this.txtQuestion.Text = model.Question;
            //this.txtAnswer.Text = model.Answer;
            //this.UserDescript.Text = model.UserDescript;
            //if (model.IsManager == 1)
            //{
            //    this.rbtnIsManager1.Checked = true;
            //}
            //else
            //{
            //    this.rbtnIsManager0.Checked = true;

            //}
            //this.IsHasExtend.Text = model.IsHasExtend;
            //this.IsHasMap.Text = model.IsHasMap;
            //this.IsHasRole.Text = model.IsHasRole;
            //this.CreateTime.Text = model.CreateTime;
            //this.RecFlag.Text = model.RecFlag;

            //this.dtpBirth.Value = Convert.ToDateTime(model.Birth);
            //this.cboPosition.SelectedValue = model.Positions;
            //this.cboEducation.SelectedValue = model.Education;

            //this.txtCellPhone.Text = model.CellPhone;
            //this.txtLibraryCard.Text = model.LibraryCard;
            //this.txtJobs.Text = model.Jobs;
            this.txtCountry.Text = model.Country;
            if (!string.IsNullOrEmpty(model.Province))
                this.cboProvince.Text = model.Province;
            if (!string.IsNullOrEmpty(model.City))
            this.cboCity.Text = model.City;
            if (!string.IsNullOrEmpty(model.County))
            this.cboCounty.Text = model.County;
            this.txtFax.Text = model.Fax;
            this.txtLoginCount.Text = model.LoginCount.ToString();

            this.cboRole.SelectedValue = model.RoleRight.ToString();
            this.txtIPFrom.Text = model.IPFrom;
            this.txtIPTo.Text = model.IPTo;
        }

        private void cboProvince_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (flagProvince)
            {
                if (this.cboProvince.SelectedIndex == -1)
                {
                    return;
                }
                else if (this.cboProvince.SelectedIndex == 0)
                {
                    InitCity();
                    InitCounty();
                }
                else
                {
                    if (this.cboCity.Items.Count > 0)
                    {
                        flagCity = false;
                        this.cboCity.DataSource = null;//此行代码可避免ComboBox控件“设置 DataSource 属性后无法修改项集合”
                        this.cboCity.Items.Clear();//清空市
                    }
                    if (this.cboCounty.Items.Count > 0)
                    {
                        this.cboCounty.DataSource = null;
                        this.cboCounty.Items.Clear();//清空区/县
                    }

                    DataRowView drv = (DataRowView)this.cboProvince.SelectedItem;
                    string codeID = drv.Row["fieldValue"].ToString();

                    flagCity = true;
                    string sqlCity = "select name as fieldName, codeID as fieldValue from [tbl_Region] where parentID='" + codeID + "'";
                    DataTable dtCity = WinLib.DBUtility.DbHelperSQL.ExecuteQuery(sqlCity);
                    this.cboCity.DataSource = dtCity;
                    this.cboCity.DisplayMember = "fieldName";
                    this.cboCity.ValueMember = "fieldValue";
                    this.cboCity.SelectedIndex = 0;
                }
            }
        }
        private void cboCity_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (flagCity)
            {
                if (this.cboCity.SelectedIndex == -1)
                {
                    return;
                }
                else if (this.cboCity.SelectedIndex >= 0)
                {
                    if (Convert.ToString(this.cboCity.SelectedValue) == "")
                    {
                        return;
                    }
                    if (this.cboCounty.Items.Count > 0)
                    {
                        this.cboCounty.DataSource = null;
                        this.cboCounty.Items.Clear();
                    }
                    DataRowView drv = (DataRowView)this.cboCity.SelectedItem;
                    string codeID = drv.Row["fieldValue"].ToString();

                    string sqlCounty = "select name as fieldName, codeID as fieldValue from [tbl_Region] where parentID='" + codeID + "'";
                    DataTable dtCounty = WinLib.DBUtility.DbHelperSQL.ExecuteQuery(sqlCounty);
                    this.cboCounty.DataSource = dtCounty;
                    this.cboCounty.DisplayMember = "fieldName";
                    this.cboCounty.ValueMember = "fieldValue";
                    this.cboCounty.SelectedIndex = 0;
                }
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Hide();
        }
    }
}
